package com.scoy.honeymei.fragment.home;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toolbar;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.scoy.honeymei.R;
import com.scoy.honeymei.base.BaseFragment;
import com.scoy.honeymei.url.MyUrl;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * ClassName:      com.scoy.honeymei.fragment.home
 * Description:
 * Author:         GaoZhan & Administrator
 * UpdateDate:     2019/10/14/0014$ 下午 14:55$
 * UpdateRemark:   更新内容
 */
public class WebFragment extends BaseFragment {


    @BindView(R.id.custom_web)
    WebView wv;
    private int typePos;
    private Context mContext;
    private Unbinder unbinder;
    private int trId;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            typePos = getArguments().getInt("type", 0);
            trId = getArguments().getInt("trId", 0);
        }
    }

    public static WebFragment newInstance(int pos, int trId) {
        Bundle args = new Bundle();
        args.putInt("type", pos);
        args.putInt("trId", trId);
        WebFragment fragment = new WebFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.custom_fragment_web, null, false);
        unbinder = ButterKnife.bind(this, view);
        mContext = getContext();

        initNormal();

        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }


    @Override
    public void initNormal() {
        initWeb(MyUrl.PREFIX_PIC+"/detail/html/attractions.html?type=" + typePos + "&id=" + trId);
    }

    public void initWeb(String meUrl) {

        wv.loadUrl(meUrl);
        //获得WebSetting,设置js有效
        WebSettings setting = wv.getSettings();
        setting.setJavaScriptEnabled(true);
        //把WebViewHelper的对象，传递到js中，供调用
        //  wv.addJavascriptInterface(new WebViewHelper(), "wvHelper");

        setting.setAppCacheEnabled(true);//启用localstorage本地存储api
        setting.setLightTouchEnabled(true);//启用选中功能
        setting.setDomStorageEnabled(true);//启用dom存储(关键就是这句)，貌似网上twitter显示有问题也是这个属性没有设置的原因
        setting.setDatabaseEnabled(true);//启用html5数据库功能

        wv.setWebChromeClient(new WebChromeClient() {
            @Override
            public boolean onJsAlert(WebView view, String url, String message,
                                     JsResult result) {
                // TODO Auto-generated method stub
                return super.onJsAlert(view, url, message, result);
            }
        });

        wv.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });

        WebSettings webSettings = wv.getSettings();
        webSettings.setDisplayZoomControls(false); //隐藏webview缩放按钮
        webSettings.setJavaScriptEnabled(true);//支持js
//        webSettings.setBuiltInZoomControls(true); // 显示放大缩小
//        webSettings.setSupportZoom(true); // 可以缩放
        wv.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                // 得到 URL 可以传给应用中的某个 WebView 页面加载显示
                return true;
            }
        });


    }


}
